package backstage.app.service.impl.util;

import java.text.ParseException;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 

import backstage.app.entity.Student;
import backstage.app.mapper.ConstitutionMapper;
import backstage.app.mapper.LevelkillStudentMapper;
import backstage.app.mapper.StudentMapper;
import backstage.app.respont.RespontDateJson;

@Component
public class StudentImplUtil {
	
	 private static final Logger logger = (Logger) LoggerFactory.getLogger(StudentImplUtil.class);
	 @Autowired
	private LevelkillStudentMapper levelkillStudentMapper;
	 @Autowired
	private ConstitutionMapper constitutionMapper;
	@Autowired
	private StudentMapper studentMapper;
	public RespontDateJson<?> inser( String name, String sex, String date, String phone,Integer studentType
			)    {
		 if(StringUtils.isEmpty(name)){
             return RespontDateJson.fuall("name="+name,"名字不能为空");
         }
         if( StringUtils.isEmpty(phone)){
             return RespontDateJson.fuall("phone="+phone,"电话不能为空");
         } 
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Student student=new Student();
		student.setName(name);
		 
		student.setSex(sex);
		 
		
		 
		try {
			student.setBirthday(sdf.parse(date));
			String[] ages=date.split("-");
			String nowtime=LocalDateTime.now().toString();
			String []nowage=nowtime.split("-"); 
			int old=Integer.parseInt(nowage[0]);
			int young=Integer.parseInt(ages[0].toString());
			int age= old-young;
			student.setAge(age);
		}catch (ParseException e) {
			 logger.error("类型转换异常");
		}
		//计算年龄
		student.setPhone(phone);
		student.setExportFlg(1);
		student.setCreateTime(LocalDateTime.now());
		student.setLastUpdateTime(LocalDateTime.now());
		student.setStudentType(studentType);
		int index=studentMapper.insertSelective(student);
		
		
		return index>0?RespontDateJson.ok(index, "添加成功"):RespontDateJson.fuall(index, "添加失败");
	}
	
	
	
	public RespontDateJson<?> update(Integer sid, String name, String sex, String birthday, String phone,Integer studentType
			) throws ParseException    {  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Student student=new Student();
		student.setSid(sid);
		student.setName(name);
		student.setSex(sex);
		student.setBirthday(sdf.parse(birthday));
		/**避免发生空指针**/
		if(birthday!=null) {
			try {
				student.setBirthday(sdf.parse(birthday));
				String[] ages=birthday.split("-");
				String nowtime=LocalDateTime.now().toString();
				String []nowage=nowtime.split("-"); 
				int old=Integer.parseInt(nowage[0]);
				int young=Integer.parseInt(ages[0].toString());
				int age= old-young;
				student.setAge(age);
			}catch (ParseException e) {
				 logger.error("to case type Exception ");
				 return RespontDateJson.fuall("","修改失败");
			}
		}	 
		//计算年龄
		student.setPhone(phone);
		 
		student.setLastUpdateTime(LocalDateTime.now());
		student.setStudentType(studentType);
		int index=studentMapper.updateByPrimaryKey(student);	
		return index>0?RespontDateJson.ok(index, "修改成功"):RespontDateJson.fuall(index,"修改失败");
	}
	
	
	public RespontDateJson<?>deleteBySid( Integer sid){
		int index=-1;
		int leaveResult=-1;
		int consResult=-1;
		try {
			 index = studentMapper.deleteByPrimaryKey(sid);
			 leaveResult= levelkillStudentMapper.deleteByPrimaryKeySid(sid);
			 consResult=constitutionMapper.deleteByPrimaryKeySid(sid);
			 logger.info("delete id success sid={},index={} ,leaveResult={}, consResult={}",sid,index,leaveResult,consResult);
			 return RespontDateJson.ok(index, "删除成功");
		} catch (Exception e) {
			logger.error(" delete student sid throws Exception  id={} ,{} {}",sid,leaveResult,consResult);
			return RespontDateJson.ok(index, "删除失败");
		}
		 
		
		 
		
	}
	
	
}
